package com.ruoyi.zyx.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.ZyxDevice;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.zyx.mapper.ZyxDeviceMapper;
import com.ruoyi.zyx.service.IZyxMemberService;
import com.ruoyi.zyx.service.IZyxOrdershareService;
import org.apache.catalina.User;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zyx.domain.ZyxSendorder;
import com.ruoyi.zyx.service.IZyxSendorderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 派工Controller
 *
 * @author lhs
 * @date 2021-06-08
 */
@RestController
@RequestMapping("/zyx/sendorder")
public class ZyxSendorderController extends BaseController
{
    @Autowired
    private IZyxSendorderService zyxSendorderService;
    @Autowired
    private ZyxDeviceMapper  zyxDeviceMapper;
    @Autowired
    private IZyxOrdershareService zyxOrdershareService;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private IZyxMemberService zyxMemberService;

    /**
     * 查询派工列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ZyxSendorder zyxSendorder)
    {
        startPage();
        List<ZyxSendorder> list = zyxSendorderService.selectZyxSendorderList(zyxSendorder);
        return getDataTable(list);
    }

    /**
     * 查询派工列表(不包含已完成的)
     */
    @GetMapping("/listHomeSendorder")
    public TableDataInfo listHomeSendorder(ZyxSendorder zyxSendorder)
    {
        startPage();
        List<ZyxSendorder> list = zyxSendorderService.selectHomeZyxSendorderList(zyxSendorder);
        return getDataTable(list);
    }

    @GetMapping("/listMySendorder")
    public TableDataInfo listMySendorder(ZyxSendorder zyxSendorder)
    {
        startPage();
//        zyxSendorder.

        zyxSendorder.setDepartid(SecurityUtils.getLoginUser().getUser().getDeptId());
        List<ZyxSendorder> list = zyxSendorderService.listMySendorder(zyxSendorder);
        return getDataTable(list);
    }
    @GetMapping("/listAllSendorder")
    public TableDataInfo listAllSendorder(ZyxSendorder zyxSendorder)
    {
        startPage();
        List<ZyxSendorder> list = zyxSendorderService.listAllSendorder(zyxSendorder);
        return getDataTable(list);
    }

    /**
     * 员工查询指派给我的订单列表
     * @param zyxSendorder
     * @return
     */
    @GetMapping("/listMyOwnSendorder")
    public TableDataInfo listMyOwnSendorder(ZyxSendorder zyxSendorder)
    {
        startPage();
//        zyxSendorder.

        zyxSendorder.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        List<ZyxSendorder> list = zyxSendorderService.listMyOwnSendorder(zyxSendorder);
        return getDataTable(list);
    }

    /**
     * 导出派工列表
     */
    @PreAuthorize("@ss.hasPermi('zyx:sendorder:export')")
    @Log(title = "派工", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ZyxSendorder zyxSendorder)
    {
        List<ZyxSendorder> list = zyxSendorderService.selectZyxSendorderList(zyxSendorder);

        ExcelUtil<ZyxSendorder> util = new ExcelUtil<ZyxSendorder>(ZyxSendorder.class);
        return util.exportExcel(list, "派工数据");
    }

    /**
     * 获取派工详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        AjaxResult ajax = AjaxResult.success();
        ZyxSendorder zyxSendorder=zyxSendorderService.selectZyxSendorderById(id);
        //获取共享人员信息
       List<Integer> integers= zyxOrdershareService.selectZyxOrdershareListByOrderId(zyxSendorder.getId());
        ajax.put(AjaxResult.DATA_TAG,zyxSendorder);
        ajax.put("selids",integers);
        List<SysUser> sysUsers=sysUserMapper.selectShareUserListByOrderId(zyxSendorder.getId());
        ajax.put("seladmins",sysUsers);
        return ajax;
    }

    /**
     * 新增派工
     */
    @PreAuthorize("@ss.hasPermi('zyx:sendorder:add')")
    @Log(title = "派工", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZyxSendorder zyxSendorder)
    {
        if(zyxSendorder.getSendtype().equals("1")){//维修
            ZyxDevice zyxDevice= zyxDeviceMapper.selectZyxDeviceById(zyxSendorder.getDeviceid());
            zyxDevice.setDevicestatus("0");//设备状态 1正常、0在修
            zyxDeviceMapper.updateZyxDevice(zyxDevice);//更新设备状态
        }


        zyxSendorder.setWxsdepartid(SecurityUtils.getLoginUser().getUser().getDeptId());
        zyxSendorder.setIssubmitcost("N");
        zyxSendorder.setIsbill("N");//是否回执
        zyxSendorder.setDevicestatus("0");//设备状态 1正常 0故障
        zyxSendorder.setServicestatus("1");//服务状态 1已派工、2 服务中、3已完成
        zyxSendorder.setSenddate(new Date());
        return toAjax(zyxSendorderService.insertZyxSendorder(zyxSendorder));
    }


    /**
     * 钉钉小程序接单
     * @param zyxSendorder
     * @return
     */
    @PostMapping(value = "/takeOrder")
    public AjaxResult takeOrder(@RequestBody ZyxSendorder zyxSendorder)
    {

        return toAjax(zyxSendorderService.takeOrder(zyxSendorder));
    }

    /**
     * 钉钉小程序开始服务
     * @param zyxSendorder
     * @return
     */
    @PostMapping(value = "/serviceOrder")
    public AjaxResult serviceOrder(@RequestBody ZyxSendorder zyxSendorder)
    {

        return toAjax(zyxSendorderService.serviceOrder(zyxSendorder));
    }

    /**
     * 钉钉小程序完成工单
     * @param zyxSendorder
     * @return
     */
    @PostMapping(value = "/overOrder")
    public AjaxResult overOrder(@RequestBody ZyxSendorder zyxSendorder)
    {

        return toAjax(zyxSendorderService.overOrder(zyxSendorder));
    }

    /**
     * 修改派工
     */
    @PreAuthorize("@ss.hasPermi('zyx:sendorder:edit')")
    @Log(title = "派工", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZyxSendorder zyxSendorder)
    {
        return toAjax(zyxSendorderService.updateZyxSendorder(zyxSendorder));
    }


    @Log(title = "派工", businessType = BusinessType.UPDATE)
    @PutMapping("/onlyupd")
    public AjaxResult onlyupd(@RequestBody ZyxSendorder zyxSendorder)
    {
        return toAjax(zyxSendorderService.onlyupd(zyxSendorder));
    }

    /**
     * 删除派工
     */
    @PreAuthorize("@ss.hasPermi('zyx:sendorder:remove')")
    @Log(title = "派工", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zyxSendorderService.deleteZyxSendorderByIds(ids));
    }
}
